package com.bawei.demo_mvp.base;

import java.lang.ref.WeakReference;

import io.reactivex.disposables.CompositeDisposable;

/**
 * @author yaotianxue
 * @description
 * @date 2025/5/12
 */
public class BasePresenter<V> {
    //强引用：不会回收 软引用：内存不足 弱引用：垃圾回收 虚引用：没有引用
    WeakReference<V> mVWeakReference;//弱引用
    protected CompositeDisposable mCompositeDisposable = new CompositeDisposable(); //网络请求链接
    //绑定View
    public void attachView(V view){
        mVWeakReference = new WeakReference<>(view);
    }
    //解除绑定
    public void distach(){
        if(mVWeakReference != null){
            mVWeakReference.clear();
            mVWeakReference = null;
        }
        if (mCompositeDisposable != null){
            mCompositeDisposable.clear();
        }
    }
    //是否绑定View
    public boolean isAttach(){
        return mVWeakReference != null && mVWeakReference.get() != null;
    }
    //返回绑定的view
    public V getView(){
        return mVWeakReference != null && mVWeakReference.get() != null ? mVWeakReference.get() : null;
    }
}
